<template>
    <div>
        <TableSearch :query="query" :options="searchOpt" :search="handleSearch" />
        <div class="container">
            <TableCustom :columns="columns" :tableData="tableData" :total="page.total" :viewFunc="handleView"
                :delFunc="handleDelete" :changePage="changePage" :editFunc="handleEdit">
                <template #toolbarBtn>
                    <el-button type="warning" :icon="CirclePlusFilled" @click="visibleRobot = true">添加机器人</el-button>
                </template>
                <template #status="{ rows }">
                    <el-tag type="success" v-if="rows.status">启用</el-tag>
                    <el-tag type="danger" v-else>关闭</el-tag>
                </template>
                <template #play="{ rows }">
                    <el-popconfirm title="此操作会设置回调，是否确认操作?" @confirm="setCallback(rows)">
                        <template #reference>
                            <el-button type="primary" size="small">设置回调</el-button>
                        </template>
                    </el-popconfirm>
                    <el-popconfirm title="此操作会删除回调，是否确认操作?" @confirm="deleteCallback(rows)">
                        <template #reference>
                            <el-button type="danger" size="small">删除回调</el-button>
                        </template>
                    </el-popconfirm>
                    <el-button type="warning" size="small" @click="handleEditBot(rows)">修改机器人</el-button>
                </template>
            </TableCustom>
        </div>
        <el-dialog title="添加机器人" v-model="visibleRobot" width="80%" destroy-on-close :close-on-click-modal="false"
            @close="closeDialog">
            <div class="robot">
                <div class="le">机器人名称</div> <el-input v-model="robot.name" placeholder="请输入机器人名称" />
            </div>
            <div class="robot">
                <div class="le">机器人key</div> <el-input v-model="robot.key" placeholder="请输入机器人key" />
            </div>
            <div class="robot">
                <div class="le">老群客服</div> <el-input v-model="robot.old_group_customer" placeholder="请输入老群客服" />
            </div>
            <div class="robot">
                <div class="le">黑名单通知客服的TG用户ID</div> <el-input v-model="robot.black_tg_chatid"
                    placeholder="请输入黑名单通知客服的TG用户ID" />
            </div>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitRobot">
                        确定
                    </el-button>
                </div>
            </template>
        </el-dialog>

    </div>
</template>

<script setup lang="ts">
    import { ref, reactive } from 'vue';
    import { ElMessage } from 'element-plus';
    import { CirclePlusFilled } from '@element-plus/icons-vue';
    import { User } from '@/types/user';
    import { agentBotList, agentAddBot, agentSetBotCallback, agentDelCallback, agentEditBot } from '@/api';
    import TableCustom from '@/components/table-custom.vue';
    import TableDetail from '@/components/table-detail.vue';
    import TableSearch from '@/components/table-search.vue';
    import { FormOption, FormOptionList } from '@/types/form-option';

    // 查询相关
    const query = reactive({
        id_str: '',
    });
    const searchOpt = ref < FormOptionList[] > ([
        { type: 'input', label: '机器人ID:', prop: 'id_str' }
    ])
    const handleSearch = () => {
        page.id_str = query.id_str
        changePage(1);
    };
    let visibleRobot = ref(false)
    // 表格相关
    let columns = ref([

        { prop: 'id_str', label: '机器人ID', width: 120 },
        { prop: 'name', label: '机器人名称', width: 120 },
        { prop: 'key', label: '机器人KEY' },
        { prop: 'status', label: '状态' },
        { prop: 'recharge_address', label: '充值地址' },
        { prop: 'today_num', label: '今日新增人数' },
        { prop: 'seven_days_num', label: '7日新增人数' },
        { prop: 'thirty_days_num', label: '30日新增人数' },
        { prop: 'sum_num', label: '总人数' },
        { prop: 'play', label: '操作项', fixed: 'right', width: 280 }
    ])

    let robot = ref({
        name: '',
        key: '',
        old_group_customer: '',
        id_str: '',
        black_tg_chatid: ''
    })

    const page = reactive({
        page: 1,
        limit: 10,
        id_str: '',
        total: 0
    })
    const tableData = ref < User[] > ([]);
    const getData = async () => {
        const res = await agentBotList(page)
        tableData.value = res.data.data;
        page.total = res.data.total
    };
    getData();

    const changePage = (val: number) => {
        page.page = val;
        getData();
    };

    // 新增/编辑弹窗相关
    let options = ref < FormOption > ({
        labelWidth: '100px',
        span: 12,
        list: [
            { type: 'input', label: '用户名', prop: 'name', required: true },
            { type: 'input', label: '手机号', prop: 'phone', required: true },
            { type: 'input', label: '密码', prop: 'password', required: true },
            { type: 'input', label: '邮箱', prop: 'email', required: true },
            { type: 'input', label: '角色', prop: 'role', required: true },
        ]
    })
    const visible = ref(false);
    const isEdit = ref(false);
    const rowData = ref({});
    const handleEdit = (row: User) => {
        rowData.value = { ...row };
        isEdit.value = true;
        visible.value = true;
    };
    const updateData = () => {
        closeDialog();
        getData();
    };

    let handleEditBot = (data) => {

        robot.value = {
            ...data
        }
        console.log(robot.value, '??');
        visibleRobot.value = true
    }
    const closeDialog = () => {
        visibleRobot.value = false;
        robot.value.name = ''
        robot.value.key = ''
        robot.value.id_str = ''
    };

    // 查看详情弹窗相关
    const visible1 = ref(false);
    const viewData = ref({
        row: {},
        list: []
    });
    const handleView = (row: User) => {
        viewData.value.row = { ...row }
        viewData.value.list = [
            {
                prop: 'id',
                label: '用户ID',
            },
            {
                prop: 'name',
                label: '用户名',
            },
            {
                prop: 'password',
                label: '密码',
            },
            {
                prop: 'email',
                label: '邮箱',
            },
            {
                prop: 'phone',
                label: '电话',
            },
            {
                prop: 'role',
                label: '角色',
            },
            {
                prop: 'date',
                label: '注册日期',
            },
        ]
        visible1.value = true;
    };

    // 删除相关
    const handleDelete = (row: User) => {
        ElMessage.success('删除成功');
    }

    // 机器人
    const submitRobot = async () => {
        if (robot.value.id_str) {
            let res = await agentEditBot(robot.value)
            if (res.code == 1) {
                getData();
                ElMessage.success(res.msg);
                closeDialog();
            }
        } else {
            let res = await agentAddBot(robot.value)
            if (res.code == 1) {
                getData();
                ElMessage.success(res.msg);
                closeDialog();
            }
        }

    }

    const setCallback = async (data) => {
        let res = await agentSetBotCallback({
            id_str: data.id_str
        })
        if (res.code == 1) {
            getData();
            ElMessage.success(res.msg);
        }
    }

    const deleteCallback = async (data) => {
        let res = await agentDelCallback({
            id_str: data.id_str
        })
        if (res.code == 1) {
            getData();
            ElMessage.success(res.msg);
        }
    }
</script>

<style scoped>
    .robot {
        margin: 20px 0;
        text-align: center;
        display: flex;
        align-items: center;

        .le {
            width: 210px;
        }
    }
</style>